<?php

class UserMessage extends Zend_Db_Table_Abstract {
	
	// private email
	const PRIVATE_MESSAGE = 1;
	// public post
	const PUBLIC_MESSAGE = 2;
	
	protected $_name = 'user_message';
	protected $_primary = 'id';
	
	protected $_referenceMap = array(
		'Message' => array(
			'columns' => 'message_id',
			'refTableClass' => 'Message',
			'refColumns' => 'id'
		),
		'TargetUser' => array(
			'columns' => 'target_user_id',
			'refTableClass' => 'User',
			'refColumns' => 'id'
		)
	);
	
	/**
	 * Find by id.
	 * 
	 * @param $id
	 * @return return user message
	 */
	public static function findById($id) {
		$userMessage = new UserMessage();
		return $userMessage->find($id)->current();
	}
	
	/**
	 * Add new user message.
	 * 
	 * @param $data
	 * @return new created user message id
	 */
	public function addUserMessage($data) {
		$userMessage = new UserMessage();
		return $userMessage->insert($data);
	}
	
	/**
	 * Get message.
	 * 
	 * @param $userMessage
	 * @return return message
	 */
	public static function getMessage($userMessage) {
		return $userMessage->findParentRow('Message');
	}
	
	/**
	 * Get user that receive the message.
	 * 
	 * @param $userMessage
	 * @return return user
	 */
	public static function getUser($userMessage) {
		return $userMessage->findParentRow('User', 'TargetUser');
	}
}
?>